﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Royaume
{
    public partial class map : System.Web.UI.Page
    {
        int meX;
        int meY;
        cSql objSql = new cSql("(local)", "DB_GAME", "RoyaumesLibres", "123456");
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["pkUser"] == null) {
                Response.Redirect("WebForm1.aspx");          
            }
            meX = 5;
            meY = 7;
            //if (!Page.IsPostBack) {
            //   this.pnMap.BackColor = System.Drawing.Color.FromName("red");
            this.dMap.Style.Add("width", "450");
            this.dMap.Style.Add("background-repeat", "no-repeat");
            this.pnMap.Width = 450;
            this.pnMap.Height = 420;
            this.fnFillMap();
            //}

        }

         protected void fnGetPosition() {
             
        }

        //5;7
        protected void fnFillMap()
        {
            // HUMAIN
            SqlDataReader myDr = this.objSql.fnStoreReader("spGetPosHumain");
             string myPosition = "";
             while (myDr.Read()) {
                 myPosition += myDr[0].ToString() + "/";
             }
             this.objSql.fnCloseConnection();
             myDr.Close();
            myPosition = myPosition.Substring(0,myPosition.Length-1);
             string[] Pos = myPosition.Split('/');
            //ELFE
             myDr = this.objSql.fnStoreReader("spGetPosElf");
             string myPositionElfe = "";
             while (myDr.Read())
             {
                 myPositionElfe += myDr[0].ToString() + "/";
             }
             myDr.Close();
             myPositionElfe = myPositionElfe.Substring(0, myPositionElfe.Length - 1);
             string[] PosElfe = myPositionElfe.Split('/');
             this.objSql.fnCloseConnection();
            //Nain
             myDr = this.objSql.fnStoreReader("spGetPosNain");
             string myPositionNain = "";
             while (myDr.Read())
             {
                 myPositionNain += myDr[0].ToString() + "/";
             }
             myDr.Close();
             myPositionNain = myPositionNain.Substring(0, myPositionNain.Length - 1);
             string[] PosNain = myPositionNain.Split('/');
             this.objSql.fnCloseConnection();
            //Mystic
             myDr = this.objSql.fnStoreReader("spGetPosMystic");
             string myPositionMistyc = "";
             while (myDr.Read())
             {
                 myPositionMistyc += myDr[0].ToString() + "/";
             }
             myDr.Close();
             myPositionMistyc = myPositionMistyc.Substring(0, myPositionMistyc.Length - 1);
             string[] PosMistyc = myPositionMistyc.Split('/');
             this.objSql.fnCloseConnection();
            ImageButton mybutton;
           
            for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    bool pass = false;
                    mybutton = new ImageButton();
                    mybutton.ID = x.ToString() + ";" + y.ToString();
                    mybutton.Height = 40;
                    mybutton.Width = 40;
                    foreach (string s in Pos) {
                        int tempoX = Convert.ToInt32(s.Substring(0, 1));
                        int tempoY = Convert.ToInt32(s.Substring(2, 1));
                        if (tempoX == x && tempoY == y)
                        {
                            mybutton.ImageUrl = "img/humainbon1.png";
                            pass = true;
                        }
                        else {
                            if (pass == false)
                            {
                                mybutton.ImageUrl = "img/transparente.png";
                            }
                        }
                        tempoX = new int();
                        tempoY = new int();
                    }
                    foreach (string z in PosElfe)
                    {
                        int tempoXelfe = Convert.ToInt32(z.Substring(0, 1));
                        int tempoYelfe = Convert.ToInt32(z.Substring(2, 1));
                        if (tempoXelfe == x && tempoYelfe == y)
                        {
                            mybutton.ImageUrl = "img/elfebon1.png";
                            pass = true;
                        }
                        else
                        {
                            if (pass == false)
                            {
                                mybutton.ImageUrl = "img/transparente.png";
                            }
                        }
                    }
                    foreach (string z in PosNain)
                    {
                        int tempoXPosNain = Convert.ToInt32(z.Substring(0, 1));
                        int tempoYPosNain = Convert.ToInt32(z.Substring(2, 1));
                        if (tempoXPosNain == x && tempoYPosNain == y)
                        {
                            mybutton.ImageUrl = "img/nainbon1.png";
                            pass = true;
                        }
                        else
                        {
                            if (pass == false)
                            {
                                mybutton.ImageUrl = "img/transparente.png";
                            }
                        }
                    }
                    foreach (string z in PosMistyc)
                    {
                        int tempoXPosMystic = Convert.ToInt32(z.Substring(0, 1));
                        int tempoYPosMystic = Convert.ToInt32(z.Substring(2, 1));
                        if (tempoXPosMystic == x && tempoYPosMystic == y)
                        {
                            mybutton.ImageUrl = "img/mysticbon1.png";
                            pass = true;
                        }
                        else
                        {
                            if (pass == false)
                            {
                                mybutton.ImageUrl = "img/transparente.png";
                            }
                        }
                    }
                    mybutton.BorderStyle = BorderStyle.Dashed;
                    mybutton.BorderWidth = 1;
                    mybutton.Attributes.Add("pos", x.ToString() + ";" + y.ToString());
                    mybutton.Click += new System.Web.UI.ImageClickEventHandler(fnMapClick);
                    this.pnMap.Controls.Add(mybutton);
                    // string pos = x.ToString() + ";" + y.ToString();

                }
            }
        }

        private void fnMapClick(object sender, EventArgs e)
        {
            ImageButton tempo;
            tempo = (ImageButton)sender;
            int fX = Convert.ToInt32(tempo.Attributes["pos"].Substring(0, 1));
            int fY = Convert.ToInt32(tempo.Attributes["pos"].Substring(2, 1));
            int diffX;
            int diffY;
            if (meX > fX)
            {
                diffX = meX - fX;
            }
            else
            {
                diffX = fX - meX;
            }

            if (meY > fY)
            {
                diffY = meY - fY;
            }
            else
            {
                diffY = fY - meY;
            }
            if (diffY > diffX)
            {
                cUtility.fnMsgBox(diffY.ToString(), Page, this);
            }
            else
            {
                cUtility.fnMsgBox(diffX.ToString(), Page, this);
            }
            /*cSql objSql = new cSql("cdelap01", "DB_USERS", "beaver21", "easton21");
            string res = objSql.fnExecSalarString("Select Prenom from t_USERS where login = 'beaver21'");
            cUtility.fnMsgBox(res, Page, this);
            */
        }
    }
}